# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.5.1
pkgrel=2
pkgdesc="Open Source Computer Vision Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://opencv.org/"
license=("BSD")
# 4.5.0 doesn't support ceres 2.x, wait for >4.5.0
depends=(#"${MINGW_PACKAGE_PREFIX}-gst-plugins-base
         "${MINGW_PACKAGE_PREFIX}-ceres-solver"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-ogre3d"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-tesseract-ocr"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-qt5"
         #"${MINGW_PACKAGE_PREFIX}-gtkglext"
         #"${MINGW_PACKAGE_PREFIX}-gtk2"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-flake8"
             "${MINGW_PACKAGE_PREFIX}-python-pylint"
             "${MINGW_PACKAGE_PREFIX}-tiny-dnn"
             #"${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-cereal"
            )
optdepends=(#"${MINGW_PACKAGE_PREFIX}-ceres-solver: SFM module"
            #"${MINGW_PACKAGE_PREFIX}-gflags: SFM module"
            #"${MINGW_PACKAGE_PREFIX}-glog: SFM module"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: support to read and write video files"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: Python 3.x interface"
            #"${MINGW_PACKAGE_PREFIX}-hdf5: opencv_hdf module"
            #"${MINGW_PACKAGE_PREFIX}-tesseract-ocr: opencv_text module"
            #"${MINGW_PACKAGE_PREFIX}-ogre3d: ovis module"
            #"${MINGW_PACKAGE_PREFIX}-vtk: opencv_viz module"
            )
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/opencv/opencv/archive/${pkgver}.tar.gz
        "${_realname}_contrib-${pkgver}.tar.gz"::https://github.com/opencv/opencv_contrib/archive/${pkgver}.tar.gz
        '0001-mingw-w64-cmake.patch'
        '0002-solve_deg3-underflow.patch'
        '0003-issue-4107.patch'
        '0004-generate-proper-pkg-config-file.patch'
        '0008-mingw-w64-cmake-lib-path.patch'
        '0010-find-libpng-header.patch'
        '0012-make-header-usable-with-C-compiler.patch'
        '0014-python-install-path.patch'
        '0015-windres-cant-handle-spaces-in-defines.patch'
        '0101-somehow-uint-not-detected.patch'
        '0102-mingw-w64-have-sincos.patch'
        '0103-sfm-module-linking.patch'
        '0104-rgbd-module-missing-include.patch')
sha256sums=('e27fe5b168918ab60d58d7ace2bd82dd14a4d0bd1d3ae182952c2113f5637513'
            '12c3b1ddd0b8c1a7da5b743590a288df0934e5cef243e036ca290c2e45e425f5'
            'e7149f3968b434e8ebe901f74eb23bc75bea49a85d2576d2773065ffa8b75650'
            'fd4e095c3c879413184fc6b91a7b0a77dbb128612341a8be2c99d804a203e362'
            '52ebc8875b9ef3ea70897f34509228daeff73d0cab0aa9eb8b931be6a7d32d7f'
            'aaf2a1952b04c9be3a133b08b0a06f536b4e9ba5f312a03f41301dba61a3fc94'
            '7398e66f80be37382bd427b5eb3a1201a23113c14e71435a44df8779ea1b8a34'
            'f4d9c95251d74165de89f9c4f17a9a265302aca10ac8723a866f413a2181a361'
            '7350e41ac9dfd95213b880ff017edda155ad704e35e024cfd9f42d286178b1fb'
            'e7ca030c5fc9d1292ff464afe5fd83ebe7d58df98321ed83dc55afba8a60ea3d'
            '6dde370fa6d557cb189dbf32cca9c5adf4e2d524f4ee12b8ae5d123514e073af'
            '7d2ff25f97c84b59793502786dd64e25c8ca991b0523ffea56b45ce031e80c3f'
            'ba227bb88c7e6948c54c5a3c8347daf5e2c930e99fb155047015bc5cfa2b33e5'
            'd9d4da3c3f0c84b80fd14b882378c79c91435db9e33b9a6199d8eb260ed6eb13'
            '3ed3f516c0a93954fdbcd08df2e685993a2cb954926524265aa13beabe699c29')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
        0001-mingw-w64-cmake.patch \
        0002-solve_deg3-underflow.patch \
        0003-issue-4107.patch \
        0004-generate-proper-pkg-config-file.patch \
        0008-mingw-w64-cmake-lib-path.patch \
        0010-find-libpng-header.patch \
        0012-make-header-usable-with-C-compiler.patch \
        0014-python-install-path.patch \
        0015-windres-cant-handle-spaces-in-defines.patch

  cd "${srcdir}/${_realname}_contrib-${pkgver}"
  apply_patch_with_msg \
        0101-somehow-uint-not-detected.patch \
        0102-mingw-w64-have-sincos.patch \
        0103-sfm-module-linking.patch \
        0104-rgbd-module-missing-include.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  CFLAGS+=" -D_POSIX_SOURCE -Wno-attributes"
  CXXFLAGS+=" -D_POSIX_SOURCE -Wno-attributes"

  export OpenEXR_HOME=${MINGW_PREFIX}
  export OpenBLAS_HOME=${MINGW_PREFIX}
  export TINYDNN_ROOT=${MINGW_PREFIX}/include/tiny_dnn

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DPYTHON2_PACKAGES_PATH=;-DPYTHON3_PACKAGES_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPKG_CONFIG_WITHOUT_PREFIX=ON \
    -DWITH_OPENCL=ON \
    -DWITH_OPENGL=ON \
    -DWITH_TBB=ON \
    -DWITH_XINE=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_PROTOBUF=OFF \
    -DPROTOBUF_UPDATE_FILES=ON \
    -DBUILD_EXAMPLES=OFF \
    -DINSTALL_C_EXAMPLES=OFF \
    -DINSTALL_PYTHON_EXAMPLES=OFF \
    -DWITH_GTK=OFF \
    -DWITH_QT=OFF \
    -DWITH_VTK=OFF \
    -DWITH_GDAL=OFF \
    -DWITH_MSMF=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_OPENJPEG=ON \
    -DWITH_OPENCL_D3D11_NV=OFF \
    -DOPENCV_SKIP_PYTHON_LOADER=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    -DOPENCV_EXTRA_MODULES_PATH=../${_realname}_contrib-${pkgver}/modules \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=ON \
    -DPYTHON_DEFAULT_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPYTHON3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPYTHON3_PACKAGES_PATH=$(cygpath -u $(${MINGW_PREFIX}/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")) \
    -DOPENCV_PYTHON3_VERSION=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))") \
    -DBUILD_opencv_bioinspired=OFF \
    -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DOPENCV_PYTHON_INSTALL_PATH=lib \
    -DOPENCV_SKIP_CMAKE_ROOT_CONFIG=ON \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR=${pkgdir} install
  
  # install missing headers https://github.com/opencv/opencv/issues/13201
  for _module in imgcodecs videoio photo; do
    cp -r "${srcdir}"/${_realname}-${pkgver}/modules/${_module}/include/opencv2/${_module}/legacy \
      "${pkgdir}${MINGW_PREFIX}"/include/opencv4/opencv2/${_module}
  done
}
